package com.gp.billsrecord.base

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.xujichang.viewbinding.ViewBindingBase
import java.lang.RuntimeException

/*列表内容中一条一条数据控制的类的基类*/
abstract class BasePagingDataAdapter<T : IComparator<T>, VB : ViewBinding> :
    PagingDataAdapter<T, BaseViewHolder<VB>>(BaseComparator()) {

    override fun onBindViewHolder(holder: BaseViewHolder<VB>, position: Int) =
        onBind(holder.binding, getItem(position))


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<VB> {
        val inflate = LayoutInflater.from(parent.context) ?: throw RuntimeException("")
        return BaseViewHolder(createBinding(inflate, parent, viewType))
    }

    abstract fun onBind(binding: VB, item: T?)
    abstract fun createBinding(inflate: LayoutInflater, parent: ViewGroup, viewType: Int): VB
}

class BaseViewHolder<VB : ViewBinding>(val binding: VB) : RecyclerView.ViewHolder(binding.root)

class BaseComparator<T1 : IComparator<T1>> : DiffUtil.ItemCallback<T1>() {
    override fun areItemsTheSame(oldItem: T1, newItem: T1): Boolean =
        oldItem.areItemsTheSame(newItem)

    override fun areContentsTheSame(oldItem: T1, newItem: T1): Boolean =
        oldItem.areContentsTheSame(newItem)
}

interface IComparator<T> {
    fun areItemsTheSame(newItem: T): Boolean
    fun areContentsTheSame(newItem: T): Boolean
}
